package com.usian.controller;

import com.usian.feign.ItemServiceFrign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/backend/item")
public class ItemControllerWeb {

    @Autowired
    private ItemServiceFrign itemServiceInterface;

    /**
     * 修改商品
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("updateTbItem")
    public Result updateTbItem(TbItem tbItem,@RequestParam String desc,@RequestParam String itemParams){
        Integer num = itemServiceInterface.updateTbItem(tbItem,desc,itemParams);
        if (num==3){
            return Result.ok();
        }
        return Result.error("修改失败");
    }

    /**
     * 回显查看
     * @param itemId
     * @return
     */
    @RequestMapping("preUpdateItem")
    public Result preUpdateItem(@RequestParam Long itemId){
        Map<String,Object> map  = itemServiceInterface.preUpdateItem(itemId);
        if (map!=null){
            return Result.ok(map);
        }
        return Result.error("回显失败");
    }

    /**
     * 添加商品
     * @param tbItem
     * @param desc
     * @param itemParams
     * @return
     */
    @RequestMapping("/insertTbItem")
    public Result insertTbItem(TbItem tbItem,@RequestParam(defaultValue = "") String desc,@RequestParam(defaultValue = "") String itemParams){
        Integer num = itemServiceInterface.insertTbItem(tbItem,desc,itemParams);
        if (num==3){
            return Result.ok();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("/findAll")
    public List<TbItem> findAll() {
       return itemServiceInterface.findAll();
    }

    /**
     * 查看商品列表
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer rows) {
        PageResult pageResult = itemServiceInterface.selectTbItemAllByPage(page,rows);
        if (pageResult.getResult()!=null && pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
       return Result.error("查无结果");
    }


    /**
     * 删除
     * @param itemId
     * @return
     */
    @RequestMapping("/deleteItemById")
    public Result deleteItemById(@RequestParam Long itemId) {
        Integer num = itemServiceInterface.deleteItemById(itemId);
        if (num==3){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 根据id查询
     * @param itemId
     * @return
     */
    @RequestMapping("/findById")
    public TbItem findById(@RequestParam Long itemId) {
        return itemServiceInterface.findById(itemId);
    }

    /**
     * 测试删除
     * @param itemId
     * @return
     */
    @RequestMapping("/delete")
    public List<TbItem> delete(@RequestParam Long itemId){
        itemServiceInterface.delete(itemId);
        return itemServiceInterface.findAll();
    }

}
